package com.example.file;

import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilterOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   Button c1,c2,c3,c4;
    ContentResolver cv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c1 = findViewById(R.id.b2);
        //c1.setOnClickListener(this);
         cv = getContentResolver();



    }

    @Override
    public void onClick(View v)
    {

        switch (v.getId())
        {
            case R.id.b2:
                try {
                    openfi();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.b3:
                xsh();
                break;
            case R.id.b4:
                try {
                    x();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;


        }
        try {
            openfi();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


   public void  openfi() throws IOException {
       // File dir = Environment.getExternalStorageDirectory();
        String path=getApplicationContext().getFilesDir().getPath();
        File p=new File("path/a.txt");
         if(!p.exists())
         {
            p.createNewFile();

         }
        try {
            FileOutputStream f=openFileOutput(p.getName(),MODE_PRIVATE);
            try {

                FileWriter writ=new FileWriter(p);
                writ.write("这是一个字符串");
                writ.flush();
                writ.close();


            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }



            FileReader re=new FileReader(p);
            Toast.makeText(this,re.read(),Toast.LENGTH_LONG);
            re.read();

            re.close();


    }
    //用 SharedPreferences保存是以xml键值对的方式保存数据

    public void xsh()
    {

        SharedPreferences p=getSharedPreferences("x",MODE_PRIVATE);
        //用editor编辑数据
        SharedPreferences.Editor ed=p.edit();
        ed.putString("name","张飒");

        ed.putInt("age",20);
        //最后要提交数据
        ed.commit();

    }
    //获取  SharedPreferences数据只需得到  SharedPreferences对象
    public void dsh()
    {
        TextView t=null;
        SharedPreferences p=getSharedPreferences("x",MODE_PRIVATE);
        String name=p.getString("name","");
        Toast.makeText(this,name,Toast.LENGTH_LONG);
        t.setText(name);
    }



   //将文件写在指定路径
    public void x() throws IOException {

        File p = new File("path/a.txt");
        if (!p.exists()) {
            p.createNewFile();

        }


        FileOutputStream s = openFileOutput(p.getName(), MODE_PRIVATE);
        BufferedOutputStream sp=new BufferedOutputStream(s);
        FileWriter fw=new FileWriter(p);
        sp.write(5);
        fw.write("这是追加");
        fw.flush();
        fw.close();


    }
    public void insc()
    {

        //Uri i=new U("cn.zut.edu.98.98");
       // cv.insert(i,)
    }
}
